*******************************************************************************
* Load data
*******************************************************************************

use dataset_gallup, clear

*******************************************************************************
* Loop over subgroups
*******************************************************************************

foreach subgroup in All Democrat Republican {
	
*******************************************************************************
* Keep the subgroup
*******************************************************************************

preserve

keep if subgroup == "`subgroup'"

*******************************************************************************
* Label lines if it's the first plot
*******************************************************************************

local legend1 legend(off)
local legend2 legend(off)
if "`subgroup'" == "All" {
local ///
legend1 legend(order(1 "{fontface monospace:{bf:Confidence in Police}}" ///
	2 "{fontface monospace:{bf:Confidence in Others}}") ///
	symysize(0) symxsize(0) ring(0) pos(11) cols(1) ///
	region(lwid(none)) fcol(none))
local ///
legend2 legend(order(1 "{fontface monospace:{bf:Difference}}") ///
	symysize(0) symxsize(0) ring(0) pos(11) cols(1) ///
	region(lwid(none)))
	
}

*******************************************************************************
* Plot raw time series
*******************************************************************************

egen conf_cf = mean(conf) if target != "police", by(year)

collapse conf*, by(target year)

levelsof target if target != "police", local(levels)
foreach l of local levels {
	#delimit;
	local mylines `mylines' (line conf_ year if target == "`l'", 
		lwid(thin) lcol(gray));
	#delimit cr
}

#delimit;

gr tw
	//`mylines'
	(line conf_ year if target == "police", lwid(thick) lcol(black))
	(line conf_cf year if target == "schools", lwid(thick) lcol(gray)) 
	,
		xline(2014, lpat(dash))
		plotregion(style(none))
		name(g1, replace)
		xtitle("")
		ytitle("")
		ylab(0.2(0.2)0.8, angle(horiz))
		xlab(1990(8)2022)
		`legend1'
		;
		
#delimit cr

gr_edit .legend.plotregion1.label[2].style.editstyle color(gray) editcopy

*******************************************************************************
* Plot differences
*******************************************************************************

egen conf_policem = mean(conf_) if target == "police", by(year)
egen conf_police = mean(conf_policem), by(year)

gen d = conf_police - conf_cf

#delimit;

gr tw
	(line d year if target == "schools", lwid(thick) lcol(black))
	(sc d year if target == "schools", msym(none))
	,
		xline(2014, lpat(dash))
		name(g2, replace)
		plotregion(style(none))
		ylab(-0.3(0.2)0.5, angle(horiz))
		xtitle("")
		ytitle("")
		yline(0, lpat(shortdash) lcol(black))
		xlab(1990(8)2022)
		`legend2'
		;
		
#delimit cr

*******************************************************************************
* Combine plots
*******************************************************************************

local mytitle `subgroup'
if "`subgroup'" != "All" local mytitle = "`subgroup's" 

gr combine g1 g2, name(G`subgroup', replace) title(`mytitle') 

*******************************************************************************
* Close subgroup loop
*******************************************************************************

restore

}

*******************************************************************************
* Combine graphs
*******************************************************************************

gr combine GAll GDemocrat GRepublican, rows(3) ysize(4) xsize(3) ///
	imargin(zero)

*******************************************************************************
* Save
*******************************************************************************

gr export "figure_gallup_dd.pdf", replace

*******************************************************************************
* End
*******************************************************************************
